package com.huika.renrench;

import java.math.BigDecimal;
import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.widget.ImageView;

import com.huika.lib.utils.DebugTool;
import com.huika.lib.utils.ImgCacheUtil;
import com.huika.renrench.config.Constant;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseApp;
import com.huika.renrench.db.areainfo.AreaInfoDaoMaster;
import com.huika.renrench.support.bean.LocationBean;
import com.huika.renrench.support.bean.LocationBean.MyLocationListener;
import com.huika.renrench.support.bean.UserModel;
import com.huika.renrench.support.bean.WebNetBean;
import com.huika.renrench.utils.GaodeMapHelper;
import com.huika.renrench.utils.LoginUtils;
import com.huika.renrench.utils.NetWorkUtils;
import com.huika.renrench.utils.PreferencesHelper;
import com.huika.renrench.utils.WebNetUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

public class GlobalApp extends BaseApp implements MyLocationListener {

	private boolean isLocated = false;
	private GaodeMapHelper gaodeMapHelper;
	/** 图片缓存滑动监听 */
	public static PauseOnScrollListener mPauseOnScrollListener;

	private static GlobalApp mInstance;
	public static UserModel userModel;
	public static ArrayList<WebNetBean> webNetBeans;
	public static String ip;
	public static String mac;
	public LocationBean locationBean;

	public static GlobalApp getInstance() {
		return mInstance;
	}

	public static UserModel getUserModel() {
		return userModel;
	}

	@Override
	public void onCreate() {
		mInstance = this;
		PreferencesHelper.getInstance();
		initLog();
		init();
		super.onCreate();
		WebNetUtils.context = getApplicationContext();
		new WebNetUtils().webNet();
		initLocation();
		initIp();
		autoLogin();
		
		IntentFilter exit_system_filter = new IntentFilter();
		exit_system_filter.addAction(Constant.ACTION_EXIT_SYSTEM);
		registerReceiver(receiverExitSystem, exit_system_filter);  
	}

	private void initIp() {
		ip = NetWorkUtils.getLocalIpAddress();
		mac = NetWorkUtils.getMacAddress(this);
	}

	public static String getIp() {
		if (ip == null || TextUtils.isEmpty(ip)) {
			ip = "192.168.0.1";
		}
		return ip;
	}

	private void init() {
		ImageLoader.getInstance().init(ImgCacheUtil.getInstance(this).getImgLoaderConfig());
		mPauseOnScrollListener = new PauseOnScrollListener(ImageLoader.getInstance(), true, true);
	}


	/**
	 * 
	 * @description：传入真名和身份证号判断是否实名认证
	 * @author zc
	 * @date 2015年6月8日 下午5:21:18
	 */
	public boolean isRealNameAuth(String realName, String idCard) {
		if (TextUtils.isEmpty(realName) || TextUtils.isEmpty(idCard)) {
			return false;
		}
		return true;
	}

	private void initLocation() {
		gaodeMapHelper = new GaodeMapHelper(this);
		gaodeMapHelper.setmLocationListener(this);
		gaodeMapHelper.start();
	}

	public static AreaInfoDaoMaster mAreaInfoDaoMaster;

	public static AreaInfoDaoMaster getAreaInfoDaoMaster() {
		if (mAreaInfoDaoMaster == null) {
			synchronized (GlobalApp.class) {
				if (mAreaInfoDaoMaster == null) {
					String path = AreaInfoDaoMaster.createOrUpdate(getInstance());
					SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
					mAreaInfoDaoMaster = new AreaInfoDaoMaster(db);
				}
			}
		}
		return mAreaInfoDaoMaster;
	}

	private void initLog() {
		DebugTool.IS_LOG = UrlConstants.IS_LOG;
		DebugTool.DEBUG_TAG = UrlConstants.DEBUG_TAG;
	}

	public String getUserId() {
		if (userModel == null) {
			return "0";
		}
		return userModel.getUserId();
	}

	/** 定位相关 *******************/
	@Override
	public void onLocation(LocationBean locationBean) {
		gaodeMapHelper.stop();
		if (!TextUtils.isEmpty(locationBean.province))
			isLocated = true;
		this.locationBean = locationBean;
	}

	public boolean isLocated() {
		return isLocated;
	}

	public void setLocated(boolean isLocated) {
		this.isLocated = isLocated;
	}

	/*********************/

	public static void loadImgLoader(ImageView view, String url) {
		ImgCacheUtil.getInstance(mInstance).displayImage(url, view);
	}

	private void autoLogin() {
		LoginUtils loginUtils = new LoginUtils(this);
		loginUtils.autoLogin();
	}

	/**
	 * 
	 * @description：可用余额
	 * @author zc
	 * @date 2015年6月9日 上午10:49:18
	 */
	public double getUseBalance(double rechargeAmount, double withdrawAmount, double investRevenue, double frozenBalance, double investAmount, double revenueFeeAmount) {
		if (userModel != null) {

			userModel.setBalance(getTotail(rechargeAmount, withdrawAmount, investRevenue, frozenBalance, investAmount, revenueFeeAmount));
			return userModel.getBalance();
		}
		return 0.00;
	}
	
	private double getTotail(double rechargeAmount, double withdrawAmount, double investRevenue, double frozenBalance, double investAmount, double revenueFeeAmount) {
		BigDecimal b1 = new BigDecimal(Double.toString(rechargeAmount));
		BigDecimal b2 = new BigDecimal(Double.toString(withdrawAmount));
		BigDecimal b3 = new BigDecimal(Double.toString(investRevenue));
		BigDecimal b4 = new BigDecimal(Double.toString(frozenBalance));
		BigDecimal b5 = new BigDecimal(Double.toString(investAmount));
		BigDecimal b6 = new BigDecimal(Double.toString(revenueFeeAmount));
		return b1.subtract(b2).add(b3).subtract(b4).subtract(b5).subtract(b6).doubleValue();
	}
	
	private BroadcastReceiver receiverExitSystem = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			GlobalApp.this.onTerminate();
		}
	};
}
